package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ZhuiAdapter;
import flc.ast.databinding.ActivityMyZhuiBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class MyZhuiActivity extends BaseAc<ActivityMyZhuiBinding> {

    private ZhuiAdapter zhuiAdapter;
    private List<StkResBean> listShow = new ArrayList<>();
    private List<StkResBean> listSel = new ArrayList<>();
    private boolean isEdit = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_my_zhui;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivMyZhuiBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivEdit.setOnClickListener(this);
        mDataBinding.tvMyZhuiCancel.setOnClickListener(this);
        mDataBinding.tvMyZhuiRemove.setOnClickListener(this);

        mDataBinding.rvMovieList.setLayoutManager(new GridLayoutManager(mContext, 3));
        zhuiAdapter = new ZhuiAdapter();
        mDataBinding.rvMovieList.setAdapter(zhuiAdapter);
        zhuiAdapter.setEdit(isEdit);
        zhuiAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivEdit://编辑
                isEdit = true;
                zhuiAdapter.setEdit(isEdit);
                zhuiAdapter.notifyDataSetChanged();
                mDataBinding.ivMyZhuiBack.setVisibility(View.GONE);
                mDataBinding.ivEdit.setVisibility(View.GONE);
                mDataBinding.tvMyZhuiCancel.setVisibility(View.VISIBLE);
                mDataBinding.tvMyZhuiRemove.setVisibility(View.VISIBLE);
                break;
            case R.id.tvMyZhuiCancel://取消
                cancelEdit();
                break;
            case R.id.tvMyZhuiRemove://移除
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_zhui));
                    return;
                }
                listShow.removeAll(listSel);
                AppSpUtils.saveMovieList(listShow);
                ToastUtils.showShort(getString(R.string.remove_suc));
                cancelEdit();
                getData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (zhuiAdapter.getItem(position).isSelected()) {
                zhuiAdapter.getItem(position).setSelected(false);
                listSel.remove(zhuiAdapter.getItem(position));
            } else {
                zhuiAdapter.getItem(position).setSelected(true);
                listSel.add(zhuiAdapter.getItem(position));
            }
            zhuiAdapter.notifyItemChanged(position);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        List<StkResBean> listSp = AppSpUtils.getMovieList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            zhuiAdapter.setList(listShow);
            mDataBinding.rvMovieList.setVisibility(View.VISIBLE);
            mDataBinding.tvMovieNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvMovieList.setVisibility(View.GONE);
            mDataBinding.tvMovieNoData.setVisibility(View.VISIBLE);
        }
    }

    private void cancelEdit() {
        isEdit = false;
        listSel.clear();
        zhuiAdapter.setEdit(isEdit);
        mDataBinding.ivMyZhuiBack.setVisibility(View.VISIBLE);
        mDataBinding.ivEdit.setVisibility(View.VISIBLE);
        mDataBinding.tvMyZhuiCancel.setVisibility(View.GONE);
        mDataBinding.tvMyZhuiRemove.setVisibility(View.GONE);
    }

}